<?php
include_once('db.class.php');
class Stock {
	const SUCCESS = 800;
	const FAIL = 0;
	const DATA_EXISTS = 1;
	
	function AddStock($name, $price) {
		$db = new db();
		$results = $db->insert("INSERT INTO stocks(stock_name, stock_price) VALUES('$name', '$price')");
		if($results != self::FAIL) {
			return self::SUCCESS;
		}
		return self::DATA_EXISTS;
	}
	
	function ViewAll() {
		$db = new db();
		$results = $db->select("SELECT * FROM stocks");
		if($results != 0) {
			return $results;
		}
		return self::FAIL;
	}
	
	function ListStocks() {
		$db = new db();
		$results = $db->select("SELECT stock_id, stock_name FROM stocks");
		if($results != 0) {
			return $results;
		}
		return self::FAIL;
	}
	
	function EditStock($id, $name, $price) {
		$db = new db();
		$results = $db->update("UPDATE stocks SET stock_name = '$name', stock_price ='$price' WHERE stock_id = '$id'");
		if($results != self::FAIL) {
			return self::SUCCESS;
		}
		return self::FAIL;
	}
	
	function DeleteStock($id) {
		$db = new db();
		$results = $db->delete("DELETE FROM stocks WHERE stock_id='$id'");
		if($results == self::SUCCESS) {
			return self::SUCCESS;
		}
		return self::FAIL;
	}
	
	function GetName($id) {
		$db = new db();
		$results = $db->select("SELECT stock_name FROM stocks WHERE stock_id = '$id'");
		if($results != 0) {
			return $results;
		}
		return self::FAIL;
	}
	
	function GetPrice($id) {
		$db = new db();
		$results = $db->select("SELECT stock_price FROM stocks WHERE stock_id = '$id'");
		if($results != 0) {
			return $results;
		}
		else {
			return self::FAIL;
		}
	}
	
	function GetMostPopular() {
		$db = new db();
		$results = $db->select("SELECT stock_name FROM stocks ORDER BY stock_count DESC LIMIT 5");
		if($results != 0) {
			return $results;
		} else {
			return self::FAIL;
		}
		
	}
	
	function AddPopularity($id) {
		$db = new db();
		$results = $db->update("UPDATE stocks SET stock_count = stock_count+1 WHERE stock_id = '$id'");
	}
}
?>